library("tidyverse")
library("firatheme")
library("prediction")
library("estimatr")
library("sandwich")
library("lmtest")
library("texreg")
library("sampleSelection")

# load data
load("~/df_replication.RData")

# Table SI 19 - Model 1
model1 <- lm_robust(timer_report ~ cue + issue + region + party_name, data = df_replication, se_type="HC2")
texreg(model1, digits = 3, include.ci = F)

estimates <- c(coef(model1)[2],coef(model1)[3],coef(model1)[4],coef(model1)[5])
lower95 <- c(confint(model1)[2,1],confint(model1)[3,1],confint(model1)[4,1],confint(model1)[5,1]) 
upper95 <- c(confint(model1)[2,2],confint(model1)[3,2],confint(model1)[4,2],confint(model1)[5,2])  
lower90 <- c(confint(model1, level = 0.90)[2,1],confint(model1, level = 0.90)[3,1],confint(model1, level = 0.90)[4,1],confint(model1, level = 0.90)[5,1])
upper90 <- c(confint(model1, level = 0.90)[2,2],confint(model1, level = 0.90)[3,2],confint(model1, level = 0.90)[4,2],confint(model1, level = 0.90)[5,2]) 
labels <- c("public opinion \n(vs. problem indicators)", "party position \n(vs. problem indicators)", "media reporting \n(vs. problem indicators)", "public schools \n(vs. climate change)")
type <- c("cue", "cue", "cue", "problem")
plotdf <- data.frame(estimates, lower95, upper95, lower90, upper90, labels, type)

 
# Figure SI 16 - left panel 
ggplot(plotdf, aes(x=labels, y=estimates, group=labels, color = type)) + 
  geom_point(size=2, position=position_dodge(width=0.3)) +
  geom_linerange(aes(ymin=lower90, ymax=upper90), size=1.5, position=position_dodge(width=0.3)) + 
  geom_linerange(aes(ymin=lower95, ymax=upper95), size=0.5, position=position_dodge(width=0.3)) + 
  coord_flip() + theme_fira() + geom_hline(yintercept=0, linetype="dashed", 
                                           color = "gray50", size=0.5) +
  scale_color_manual(values=c("gray50", "gray50")) +
  theme(legend.position= "none") + labs(x="", y="Δ time exposure \n(in seconds)", title="", caption = "Note: Error bars represent 90% and 95% Confidence Intervals") +
  ylim(-100, 100) + facet_wrap(~type, ncol = 1, scales = "free_y") +
  theme(axis.title = element_text(size=6), legend.position= "none", plot.caption = element_text(size = 6, color = "gray40", face = "italic"))


# Table SI 19 - Model 2
df_replication1 <- subset(df_replication, timer_report <= 1500)

model2 <- lm_robust(timer_report ~ cue + issue + region + party_name, data = df_replication1, se_type="HC2")
summary(model2)
texreg(model2, digits = 3, include.ci = F)

estimates <- c(coef(model2)[2],coef(model2)[3],coef(model2)[4],coef(model2)[5])
lower95 <- c(confint(model2)[2,1],confint(model2)[3,1],confint(model2)[4,1],confint(model2)[5,1]) 
upper95 <- c(confint(model2)[2,2],confint(model2)[3,2],confint(model2)[4,2],confint(model2)[5,2])  
lower90 <- c(confint(model2, level = 0.90)[2,1],confint(model2, level = 0.90)[3,1],confint(model2, level = 0.90)[4,1],confint(model2, level = 0.90)[5,1])
upper90 <- c(confint(model2, level = 0.90)[2,2],confint(model2, level = 0.90)[3,2],confint(model2, level = 0.90)[4,2],confint(model2, level = 0.90)[5,2]) 
labels <- c("public opinion \n(vs. problem indicators)", "party position \n(vs. problem indicators)", "media reporting \n(vs. problem indicators)", "public schools \n(vs. climate change)")
type <- c("cue", "cue", "cue", "problem")
plotdf <- data.frame(estimates, lower95, upper95, lower90, upper90, labels, type)

# Figure SI 16 - right panel  
ggplot(plotdf, aes(x=labels, y=estimates, group=labels, color = type)) + 
  geom_point(size=2, position=position_dodge(width=0.3)) +
  geom_linerange(aes(ymin=lower90, ymax=upper90), size=1.5, position=position_dodge(width=0.3)) + 
  geom_linerange(aes(ymin=lower95, ymax=upper95), size=0.5, position=position_dodge(width=0.3)) + 
  coord_flip() + theme_fira() + geom_hline(yintercept=0, linetype="dashed", 
                                           color = "gray50", size=0.5) +
  scale_color_manual(values=c("gray50", "gray50")) +
  theme(legend.position= "none") + labs(x="", y="Δ time exposure \n(in seconds)", title="", caption = "Note: Error bars represent 90% and 95% Confidence Intervals") +
  ylim(-25, 25) + facet_wrap(~type, ncol = 1, scales = "free_y") +
  theme(axis.title = element_text(size=6), legend.position= "none", plot.caption = element_text(size = 6, color = "gray40", face = "italic"))

# Table SI 20
load("~/df_replication.RData")

df_replication$log_timer <- ifelse(df_replication$timer_report > 0, log(df_replication$timer_report), 0)
model3 <- lm_robust(log_timer ~ cue + issue + region + party_name, data = df_replication, se_type="HC2")
texreg(model3, digits = 3, include.ci = F)

estimates <- c(coef(model3)[2],coef(model3)[3],coef(model3)[4],coef(model3)[5])
lower95 <- c(confint(model3)[2,1],confint(model3)[3,1],confint(model3)[4,1],confint(model3)[5,1]) 
upper95 <- c(confint(model3)[2,2],confint(model3)[3,2],confint(model3)[4,2],confint(model3)[5,2])  
lower90 <- c(confint(model3, level = 0.90)[2,1],confint(model3, level = 0.90)[3,1],confint(model3, level = 0.90)[4,1],confint(model3, level = 0.90)[5,1])
upper90 <- c(confint(model3, level = 0.90)[2,2],confint(model3, level = 0.90)[3,2],confint(model3, level = 0.90)[4,2],confint(model3, level = 0.90)[5,2]) 
labels <- c("public opinion \n(vs. problem indicators)", "party position \n(vs. problem indicators)", "media reporting \n(vs. problem indicators)", "public schools \n(vs. climate change)")
type <- c("cue", "cue", "cue", "problem")
plotdf <- data.frame(estimates, lower95, upper95, lower90, upper90, labels, type)

# Figure SI 17 
ggplot(plotdf, aes(x=labels, y=estimates, group=labels, color = type)) + 
  geom_point(size=2, position=position_dodge(width=0.3)) +
  geom_linerange(aes(ymin=lower90, ymax=upper90), size=1.5, position=position_dodge(width=0.3)) + 
  geom_linerange(aes(ymin=lower95, ymax=upper95), size=0.5, position=position_dodge(width=0.3)) + 
  coord_flip() + theme_fira() + geom_hline(yintercept=0, linetype="dashed", 
                                           color = "gray50", size=0.5) +
  scale_color_manual(values=c("gray50", "gray50")) +
  theme(legend.position= "none") + labs(x="", y="Δ time exposure \n(in seconds) \nlog transformed", title="", caption = "Note: Error bars represent 90% and 95% Confidence Intervals") +
  ylim(-1, 1) + facet_wrap(~type, ncol = 1, scales = "free_y") +
  theme(axis.title = element_text(size=6), legend.position= "none", plot.caption = element_text(size = 6, color = "gray40", face = "italic"))



load("~/df_replication.RData")
df_replication1 <- subset(df_replication, timer_report <= 1500)

# selection model
heck1 <- heckit(click ~ cue + issue + region + party_name,
                timer_report ~ cue + issue, data=df_replication1)

estimates <- c(coef(heck1)[20],coef(heck1)[21],coef(heck1)[22],coef(heck1)[23])
lower95 <- c(confint(heck1$lm)[2,1],confint(heck1$lm)[3,1],confint(heck1$lm)[4,1],confint(heck1$lm)[5,1]) 
upper95 <- c(confint(heck1$lm)[2,2],confint(heck1$lm)[3,2],confint(heck1$lm)[4,2],confint(heck1$lm)[5,2])  
lower90 <- c(confint(heck1$lm, level = 0.90)[2,1],confint(heck1$lm, level = 0.90)[3,1],confint(heck1$lm, level = 0.90)[4,1],confint(heck1$lm, level = 0.90)[5,1])
upper90 <- c(confint(heck1$lm, level = 0.90)[2,2],confint(heck1$lm, level = 0.90)[3,2],confint(heck1$lm, level = 0.90)[4,2],confint(heck1$lm, level = 0.90)[5,2]) 
labels <- c("public opinion \n(vs. problem indicators)", "party position \n(vs. problem indicators)", "media reporting \n(vs. problem indicators)", "public schools \n(vs. climate change)")
type <- c("cue", "cue", "cue", "problem")
plotdf <- data.frame(estimates, lower95, upper95, lower90, upper90, labels, type)

#main plot
ggplot(plotdf, aes(x=labels, y=estimates, group=labels, color = type)) + 
  geom_point(size=2, position=position_dodge(width=0.3)) +
  geom_linerange(aes(ymin=lower90, ymax=upper90), size=1.5, position=position_dodge(width=0.3)) + 
  geom_linerange(aes(ymin=lower95, ymax=upper95), size=0.5, position=position_dodge(width=0.3)) + 
  coord_flip() + theme_fira() + geom_hline(yintercept=0, linetype="dashed", 
                                           color = "gray50", size=0.5) +
  scale_color_manual(values=c("gray50", "gray50")) +
  theme(legend.position= "none") + labs(x="", y="Δ time exposure \n(in seconds)", title="", caption = "Note: Error bars represent 90% and 95% Confidence Intervals") +
  ylim(-50, 50) + facet_wrap(~type, ncol = 1, scales = "free_y") +
  theme(axis.title = element_text(size=6), legend.position= "none", plot.caption = element_text(size = 6, color = "gray40", face = "italic"))


df_replication1$log_timer <- ifelse(df_replication1$timer_report > 0, log(df_replication1$timer_report), 0)
heck2 <- heckit(click ~ cue + issue + region + party_name,
                log_timer ~ cue + issue, data=df_replication1)


estimates <- c(coef(heck2)[20],coef(heck2)[21],coef(heck2)[22],coef(heck2)[23])
lower95 <- c(confint(heck2$lm)[2,1],confint(heck2$lm)[3,1],confint(heck2$lm)[4,1],confint(heck2$lm)[5,1]) 
upper95 <- c(confint(heck2$lm)[2,2],confint(heck2$lm)[3,2],confint(heck2$lm)[4,2],confint(heck2$lm)[5,2])  
lower90 <- c(confint(heck2$lm, level = 0.90)[2,1],confint(heck2$lm, level = 0.90)[3,1],confint(heck2$lm, level = 0.90)[4,1],confint(heck2$lm, level = 0.90)[5,1])
upper90 <- c(confint(heck2$lm, level = 0.90)[2,2],confint(heck2$lm, level = 0.90)[3,2],confint(heck2$lm, level = 0.90)[4,2],confint(heck2$lm, level = 0.90)[5,2]) 
labels <- c("public opinion \n(vs. problem indicators)", "party position \n(vs. problem indicators)", "media reporting \n(vs. problem indicators)", "public schools \n(vs. climate change)")
type <- c("cue", "cue", "cue", "problem")
plotdf <- data.frame(estimates, lower95, upper95, lower90, upper90, labels, type)

#main plot
ggplot(plotdf, aes(x=labels, y=estimates, group=labels, color = type)) + 
  geom_point(size=2, position=position_dodge(width=0.3)) +
  geom_linerange(aes(ymin=lower90, ymax=upper90), size=1.5, position=position_dodge(width=0.3)) + 
  geom_linerange(aes(ymin=lower95, ymax=upper95), size=0.5, position=position_dodge(width=0.3)) + 
  coord_flip() + theme_fira() + geom_hline(yintercept=0, linetype="dashed", 
                                           color = "gray50", size=0.5) +
  scale_color_manual(values=c("gray50", "gray50")) +
  theme(legend.position= "none") + labs(x="", y="Δ time exposure \n(in seconds)", title="", caption = "Note: Error bars represent 90% and 95% Confidence Intervals") +
  ylim(-1, 1) + facet_wrap(~type, ncol = 1, scales = "free_y") +
  theme(axis.title = element_text(size=6), legend.position= "none", plot.caption = element_text(size = 6, color = "gray40", face = "italic"))


